package com.k2.whereareyou.web;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import com.k2.whereareyou.xml.PhotoXML;

import android.os.Environment;
import android.util.Log;

public class RequestController {
	
	
	
	public static String getResponseByXml(String urlText,Object xmlObject){
		
		String strXML = objectToString(xmlObject);
		if (strXML==null)
			return null;

    	Log.d("reqestStr", strXML);
		
//		  HttpURLConnection connection = null;
//		  try{
//			  URL url = new URL(urlText);
//			  connection = (HttpURLConnection)url.openConnection();
//		   if (connection == null)
//		    return null;
//		   
//		   connection.setRequestMethod("POST");
//		   connection.setUseCaches(false);
//		   connection.setDoOutput(true);
//		   connection.setDoInput(true);
//		   connection.setRequestProperty("User-Agent", "Android/2.1");
//		   connection.setRequestProperty("Content-Language", "ru-RU");
//		   connection.setRequestProperty("Connection", "close");
//		   String BOUNDRY = "123QUY75ER54RFDS";
//		   connection.setRequestProperty("Content-Type", "application/octet-stream; boundary=" + BOUNDRY);
//		   
//		   StringBuffer requestBody = new StringBuffer();
//
//		   requestBody.append("--");
//		   requestBody.append(BOUNDRY);
//		   requestBody.append('\n');
//		   
//		   requestBody.append("Content-Disposition: form-data; name='xml'\n");
//		   requestBody.append("Content-Type: application/octet-stream\n");
//		   requestBody.append('\n');
//
//		   requestBody.append( strXML );
//
//		   requestBody.append("\n");
//		   requestBody.append("--");
//		   requestBody.append(BOUNDRY);
//		   requestBody.append("--");
//		   requestBody.append("\n");
//
//		   connection.setRequestProperty("Content-Length",
//		                                   String.valueOf(requestBody.toString().length()));
//		   connection.connect();
//
//		   DataOutputStream dataOS = new DataOutputStream( connection.getOutputStream() );
//		   dataOS.writeBytes(requestBody.toString());
//
//		   dataOS.flush();
//		   dataOS.close();
		   
		   return "ok";
//		   int responseCode = connection.getResponseCode();
//		   
//		   if (responseCode == HttpURLConnection.HTTP_OK)
//		   	{
//		       InputStream in = connection.getInputStream();
//		       InputStreamReader isr = new InputStreamReader(in,"windows-1251");
//		    
//		       StringBuffer data = new StringBuffer();
//		       int ch;
//		       while ((ch = isr.read()) != -1)
//		       {
//		           data.append((char) ch);
//		       }
//		    
//		       String resultString = new String(data.toString());
//		       return resultString;
//		   	} 
//		   	else {
//		   		return null;
//		    }
//		  }
//		  catch (Exception e1) {
//			  return null;
//			    }
		  }
	
	private static String objectToString(Object xmlObject){
		Writer writer = new StringWriter();
		Serializer serializer = new Persister();
		try{
			serializer.write(xmlObject, writer);
		    String strXML = writer.toString();
		    return strXML;
		}
		catch (Exception e) 
		{
		    return null;
		}
	}

	public static String getResponseByXmlPhoto(String urlText, PhotoXML photoXML,
			String path) {

		String strXML = objectToString(photoXML);
		if (strXML==null)
			return null;

    	Log.d("reqestStr", strXML);
		
//		  HttpURLConnection connection = null;
//		  try{
//			  URL url = new URL(urlText);
//			  connection = (HttpURLConnection)url.openConnection();
//		   if (connection == null)
//		    return null;
//		   
//		   connection.setRequestMethod("POST");
//		   connection.setUseCaches(false);
//		   connection.setDoOutput(true);
//		   connection.setDoInput(true);
//		   connection.setRequestProperty("User-Agent", "Android/2.1");
//		   connection.setRequestProperty("Content-Language", "ru-RU");
//		   connection.setRequestProperty("Connection", "close");
//		   String BOUNDRY = "123QUY75ER54RFDS";
//		   connection.setRequestProperty("Content-Type", "application/octet-stream; boundary=" + BOUNDRY);
//		   
//		   StringBuffer requestBody = new StringBuffer();
//
//		   requestBody.append("--");
//		   requestBody.append(BOUNDRY);
//		   requestBody.append('\n');
//		   
//		   requestBody.append("Content-Disposition: form-data; name='xml'\n");
//		   requestBody.append("Content-Type: application/octet-stream\n");
//		   requestBody.append('\n');
//
//		   requestBody.append( strXML );
//
//		   requestBody.append("\n");
//		   requestBody.append("--");
//		   requestBody.append(BOUNDRY);
//		   requestBody.append('\n');
//		   
//		   requestBody.append("Content-Disposition: form-data; name='jpg'\n");
//		   requestBody.append("Content-Type: application/octet-stream\n");
//		   requestBody.append('\n');
//		   
//		   byte[] byteJpg = getJpgByByte(path);
//		   if (byteJpg == null){
//			   return null;
//		   }
//		   requestBody.append( byteJpg );
//
//		   requestBody.append("\n");
//		   requestBody.append("--");
//		   requestBody.append(BOUNDRY);
//		   requestBody.append("--");
//		   requestBody.append("\n");
//
//		   connection.setRequestProperty("Content-Length",
//		                                   String.valueOf(requestBody.toString().length()));
//		   connection.connect();
//
//		   DataOutputStream dataOS = new DataOutputStream( connection.getOutputStream() );
//		   dataOS.writeBytes(requestBody.toString());
//
//		   dataOS.flush();
//		   dataOS.close();
//		   
//		   int responseCode = connection.getResponseCode();
//		   
//		   if (responseCode == HttpURLConnection.HTTP_OK)
//		   	{
//		       InputStream in = connection.getInputStream();
//		       InputStreamReader isr = new InputStreamReader(in,"windows-1251");
//		    
//		       StringBuffer data = new StringBuffer();
//		       int ch;
//		       while ((ch = isr.read()) != -1)
//		       {
//		           data.append((char) ch);
//		       }
//		    
//		       String resultString = new String(data.toString());
//		       return resultString;
//		   	} 
//		   	else {
//		   		return null;
//		    }
//		  Log.d("reqestStr_2", requestBody.toString());
//		  }
//		  catch (Exception e1) {
//			  return null;
//			    }
				
		  return "ok";
		
	}
	
	private static byte[] getJpgByByte(String path){
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try{
			FileInputStream fis = new FileInputStream(path);
			byte[] buf = new byte[1024];
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum); 
            }
            byte[] bytes = bos.toByteArray();
    		return bytes;
        } catch (IOException ex) {
        }
        return null;
	}
	
}
